import java.awt.Color;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RemoteException;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

import java.rmi.Naming;

public class PanelNoeud extends JPanel {
	
	NoeudInterface noeud;
	
	JButton jDemanderSC;
	JButton jLibererSC;
	ImageIcon flecheRouge = new ImageIcon("img/fleche_rouge.png");
	ImageIcon flecheVerte = new ImageIcon("img/fleche_verte.png");
	ImageIcon pointRouge = new ImageIcon("img/point_rouge.png");
	ImageIcon pointVert = new ImageIcon("img/point_vert.png");
	
	JLabel estRacine = new JLabel("", JLabel.CENTER);
	JLabel demandeSC = new JLabel("", JLabel.CENTER);
	
	JComboBox listePeres = new JComboBox();
	
	
	public PanelNoeud(Integer id, int total) {
		super();
		
		// Création du noeud
		try {
			Naming.rebind("rmi://127.0.0.1:1099/"+id.toString(), new Noeud(id.toString(), false, this));
			noeud = (NoeudInterface)Naming.lookup("rmi://127.0.0.1:1099/"+id.toString());
		}
		catch(Exception e) {
		      System.out.println("Serveur en panne"+e);
	    }
		
		setLayout(new GridLayout(0,3));
		
		setMaximumSize(new Dimension(500, 90));		
		setMinimumSize(new Dimension(500, 90));
		setBorder(BorderFactory.createMatteBorder(3, 0, 0, 0,Color.black));
		
		add(new JLabel("Noeud "+id));
		add(new JLabel("Père : ", JLabel.RIGHT));
		
		// Construction de la selection du père
		for(Integer i=0;i<total;i++){
			if(i != id){
				listePeres.addItem("Noeud "+i.toString());
			}
		}
		listePeres.addItem("est racine");
		
		// ACTION LISTENER LISTE DES PERES
		listePeres.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JComboBox source = (JComboBox)e.getSource();
				PanelNoeud panel = (PanelNoeud)source.getParent();
				if(source.getSelectedItem().toString() == "est racine"){
					panel.estRacine(true);
					panel.setPere(null);
				}
				else{					
					panel.estRacine(false);
					Integer id_pere = Integer.parseInt(source.getSelectedItem().toString().replace("Noeud ", ""));
					panel.setPere(((Administrateur)panel.getRootPane().getParent()).getNoeud(id_pere));
				}
					
				
			}
		});
		// ---------------------------------		
		
		add(listePeres);
		add(new JLabel(""));
		add(new JLabel("est Racine ?", JLabel.CENTER));
		add(new JLabel("demande SC ?", JLabel.CENTER));
		
		jDemanderSC = new JButton("Demander SC");
		jDemanderSC.setIcon(flecheRouge);
		
		
		// ACTION LISTENER DEMANDER LA SC
		jDemanderSC.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JButton source = (JButton)e.getSource();
				PanelNoeud panel = (PanelNoeud)source.getParent();	
				
				try {
					panel.getNoeud().demandeSectionCritique();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				panel.demandeSC(true);
				panel.autoriserDemande(false);
				
				((Administrateur)panel.getRootPane().getParent()).interdireSelectionPeres();
			}
		});
		//-------------------------------------
		
		
		add(jDemanderSC);
		JLabel racine = new JLabel("", JLabel.CENTER);
		racine.setIcon(flecheRouge);
		add(estRacine);
		add(demandeSC);
		jLibererSC = new JButton("Libérer SC");
		jLibererSC.setIcon(flecheVerte);
		
		
		// ACTION LISTENER LIBERE LA SC
		jLibererSC.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JButton source = (JButton)e.getSource();
				PanelNoeud panel = (PanelNoeud)source.getParent();	
				
				try {
					noeud.sortSC();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				panel.demandeSC(false);
				panel.autoriserDemande(true);
			}
		});
		//-------------------------------------
		
		add(jLibererSC);		
		
		estRacine(false);
		demandeSC(false);		
		autoriserDemande(true);
		
		setVisible(true);	
		
	}
	
	public void init(){
		listePeres.setSelectedIndex(0);
		jLibererSC.setEnabled(false);
	}
	
	public void autoriserDemande(boolean b){
		jDemanderSC.setEnabled(b);		
	}
	
	public void autoriserRendreSC(){
		if(estRacine.getIcon() == pointVert  && demandeSC.getIcon() == pointVert){
			jLibererSC.setEnabled(true);
		}
		else{
			jLibererSC.setEnabled(false);
		}
	}
	
	public void estRacine(boolean b){
		if(b){
			estRacine.setIcon(pointVert);
			autoriserRendreSC();
		}
		else{
			estRacine.setIcon(pointRouge);
			autoriserRendreSC();
		}
	}
	
	public void demandeSC(boolean b){
		if(b){
			demandeSC.setIcon(pointVert);
			autoriserRendreSC();
		}
		else{
			demandeSC.setIcon(pointRouge);
			autoriserRendreSC();
		}
	}
	
	public void autoriserSimulation(boolean b){
		jDemanderSC.setEnabled(b);
		jLibererSC.setEnabled(b);
	}
	
	public void autoriserSelectionPere(boolean b){
		listePeres.setEnabled(b);
	}
	
	public void setPere(NoeudInterface noeudInterface){
		try {
			noeud.setPere(noeudInterface);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public NoeudInterface getNoeud(){
		return noeud;
	}

}